package demo2;

class ListNode {
    int val;
    ListNode prev;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}
// 2、无头双向链表实现
public class MyLinkedList {
    ListNode head;
    ListNode last;
    //打印链表
    public void display(){
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //头插法
    public void addFirst(int data){
        ListNode newNode = new ListNode(data);
        if (head == null) {
            this.head = newNode;
            this.last = newNode;
            return;
        }
        this.head.prev = newNode;
        newNode.next = this.head;
        this.head = newNode;
    }
    //尾插法
    public void addLast(int data){
        ListNode newNode = new ListNode(data);
        if (head == null) {
            this.head = newNode;
            this.last = newNode;
            return;
        }
        this.last.next = newNode;
        newNode.prev = this.last;
        this.last = newNode;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        ListNode newNode = new ListNode(data);
        //通过findIndex方法找到data的那个节点
        ListNode cur = findIndex(data);
        cur.prev.next = newNode;
        newNode.prev = cur.prev;
        cur.prev = newNode;
        newNode.next = cur;

    }
    public ListNode findIndex (int index) {
        ListNode cur = this.head;
        while (index-- > 0) {
            cur = cur.next;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur != head) {
                    //中间  尾巴
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        //如果删的是最后一个元素 那么把last往前移动一个.
                        this.last = this.last.prev;
                    }
                } else {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    }
                }
                return;
            }
            cur = cur.next;

        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur != head) {
                    //中间  尾巴
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        //如果删的是最后一个元素 那么把last往前移动一个.
                        this.last = this.last.prev;
                    }
                } else {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    }
                }
            }
            cur = cur.next;

        }
    }
    //得到双向链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void clear(){
        ListNode cur = this.head;
        while (this.head != null) {
            cur = cur.next;
            this.head.prev = null;
            this.head.next = null;
            this.head = cur;
        }
    }
}
